package ch.fhnw.qualiscan.core.helpers;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.QuickscanApplication;
import ch.fhnw.qualiscan.core.db.pojos.Survey;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.core.db.pojos.User;
import eu.livotov.tpt.TPTApplication;
import eu.livotov.tpt.i18n.TM;

public class EmailHelper {
	public Hash hashHelper;
	
	public EmailHelper() {
		hashHelper = new Hash();
	}
	
	public boolean sendEmail(String to, String subject, String message) { 
		try {
			Properties props = new Properties(); 
		    props.put("mail.smtp.host", Config.EMAIL_SMTP); 
		    Session session = Session.getDefaultInstance(props); 
		    
		    Message msg = new MimeMessage(session); 
		    InternetAddress addressFrom = new InternetAddress(Config.EMAIL_SENDER); 
		    msg.setFrom(addressFrom); 
		    
		    InternetAddress addressTo = new InternetAddress(to); 
		    msg.setRecipient(Message.RecipientType.TO, addressTo); 
		    msg.setSubject(subject); 
		    
		    msg.setContent(message, "text/html"); 
		    msg.setHeader("Content-Type", "text/html; charset=UTF-8");
		    
		    Transport.send(msg); 
		    
		    return true;
		}
		catch(Exception MessagingException) {
			MessagingException.printStackTrace();
			return false;
		}
	}
	
	/**
	 * Generic function for sending emails.
	 * @param to
	 * @param from
	 * @param subject
	 * @param message
	 * @return
	 */
	public boolean sendEmail(String to, String from, String subject, String message) {
		try {
			Properties props = new Properties(); 
		    props.put("mail.smtp.host", Config.EMAIL_SMTP); 
		    Session session = Session.getDefaultInstance(props); 
		    
		    Message msg = new MimeMessage(session); 
		    
		    InternetAddress addressFrom = new InternetAddress(Config.EMAIL_SENDER); 
		    msg.setFrom(addressFrom); 
		    
		    InternetAddress addressTo = new InternetAddress(to); 
		    msg.setRecipient(Message.RecipientType.TO, addressTo); 
		    msg.setSubject(subject); 
		    
		    msg.setContent(message, "text/html"); 
		    msg.setHeader("Content-Type", "text/html; charset=UTF-8");
		    Transport.send(msg); 
		    
		    return true;
		}
		catch(Exception MessagingException) {
			MessagingException.printStackTrace();
			return false;
		}
	}
	
	/**
	 * E-Mail which is being sent when signing up, containing the login credentials.
	 * @param user
	 */
	public void sendConfirmationMail(User user) {
		String body = 
			TM.get("email.hello", user.getFirstname() + " " + user.getLastname())
			+ "<br /> <br />"
			+ TM.get("email.signedup.body") + "" 
			+ " <br /><br />"
			+ TM.get("user.username") + ": " + user.getEmail() 
			+ "<br />"
			+ TM.get("user.password") + ": " + user.getPassword()
			+ " <br /><br />" 
			+ Config.LANDING_URL;
		
		sendEmail(user.getEmail(), TM.get("email.signedup.subject"), body);
	}
	
	/**
	 * E-Mail which is being sent containing the login credentials of the user
	 * @param user
	 */
	public void sendPasswordMail(User user) {
		String body = 
			TM.get("general.hello", user.getFirstname() + " " + user.getLastname())
			+ "<br /> <br />"
			+ TM.get("email.pwdforgot.body") + "" 
			+ " <br /><br />"
			+ TM.get("user.username") + ": " + user.getEmail() 
			+ "<br />"
			+ TM.get("user.password") + ": " + user.getPassword()
			+ "<br />"
			+ Config.LANDING_URL
			+ " <br /><br />" 
			+ TM.get("email.goodbye");
		
		sendEmail(user.getEmail(), TM.get("email.pwdforgot.subject"), body);
	}
	
	/**
	 * E-Mail reminding all the participants, when the completion date has been changed.
	 * @param user
	 * @param survey
	 */
	public void sendSurveyCompletionDateChanged(User user, Survey survey) {
		if(user == null || survey == null) 
			return;
		
		String name = user.getFirstname() + " " + user.getLastname();
		String surveyName = survey.getTitle();
		String surveyDate = survey.getCompletiondate().toString();
		
		String body = TM.get("email.surveychanged.body", name, surveyName, surveyDate);
		this.sendEmail(user.getEmail(), TM.get("email.surveychanged.subject"), body);
	}
	
	/**
	 * E-Mail reminding the Coordinator one day before the completion date. Fired by the crob-job.
	 * @param user
	 * @param survey
	 */
	public void sendCoordinatorReminderEmail(User user, Survey survey) {
		TM.setDefaultLanguage(user.getLanguage().getLocale());
		
		String body = TM.get("email.coordinatorreminder.text", " " + user.getFirstname()+" "+user.getLastname()+"", survey.getTitle());
		sendEmail(user.getEmail(), TM.get("email.coordinatorreminder.subject", survey.getTitle()), body);
	}
	
	/**
	 * E-Mail which is being sent, when signing up for QualiScan
	 * @param sur
	 * @param text
	 */
	public void sendInvitationMail(SurveyUserRole sur, String text) {
		String thirdParameter = ""+sur.getId()+"@"+hashHelper.generateSurHash(sur.getSurvey().getId(), sur.getUser().getId(), sur.getRole().getId())+"";
		String Link = Config.BASE_URL + "#Invitation/0/" + thirdParameter;
		String mailText = text + "<br /><br />" + Link
			+ "<br /><br />"
			+ TM.get("email.goodbye");
		
		sendEmail(sur.getUser().getEmail(), TM.get("email.invitation.subject"), mailText);
	}
	
}
